home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 22 / CU Amiga Magazine's Super CD-ROM 22 (1998)(EMAP Images)(GB)[!][issue 1998-05].iso / PowerPC / Programming / PPCsiod / sources / sym_clo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  6.3 KB  |  231 lines

  1. /* Scheme In One Define.
  2.  
  3. The garbage collector, the name and other parts of this program are
  4.  
  5.  *                     COPYRIGHT (c) 1989 BY                              *
  6.  *      PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
  7.  
  8. Conversion  to  full scheme standard, characters, vectors, ports, complex &
  9. rational numbers, and other major enhancments by
  10.  
  11.  *      Scaglione Ermanno, v. Pirinoli 16 IMPERIA P.M. 18100 ITALY        * 
  12.  
  13. Permission  to use, copy, modify, distribute and sell this software and its
  14. documentation  for  any purpose and without fee is hereby granted, provided
  15. that  the  above  copyright  notice appear in all copies and that both that
  16. copyright   notice   and   this  permission  notice  appear  in  supporting
  17. documentation,  and that the name of Paradigm Associates Inc not be used in
  18. advertising or publicity pertaining to distribution of the software without
  19. specific, written prior permission.
  20.  
  21. PARADIGM  DISCLAIMS  ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  22. ALL  IMPLIED  WARRANTIES  OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  23. PARADIGM  BE  LIABLE  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  24. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  25. IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  26. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <setjmp.h>
  34. #include <signal.h>
  35. #include <math.h>
  36.  
  37. #include "siod.h"
  38.  
  39. LISP symcons(char *pname, LISP vcell)
  40. {LISP z;
  41.  NEWCELL(z,tc_symbol);
  42.  PNAME(z) = pname;
  43.  (*z).storage_as.symbol.vcell = cons(vcell,NIL);
  44.  return(z);}
  45.  
  46. LISP gensym(LISP arg)
  47. {LISP z;
  48.  char *p;
  49.  long flag;
  50.  static long counter = 0;
  51.  static char prefix[80] = "g";
  52.  if(NSTRINGP(arg) && NINTNUMP(arg) && NNULLP(arg))
  53.     err("arg to gensym must be a string or an integer",arg,ERR_GEN);
  54.  flag = no_interrupt(1);
  55.  if(STRINGP(arg))
  56.    {if(strlen(SNAME(arg))>79)
  57.       err("string too long to gensym",arg,ERR_GEN);
  58.     strcpy(prefix,SNAME(arg));}
  59.  else if(INTNUMP(arg)&&(INTNM(arg)>0))
  60.    counter = INTNM(arg);
  61.  sprintf(tkbuffer,"%s%d",prefix,counter);
  62.  z = strcons(strlen(tkbuffer)+1);
  63.  strcpy(SNAME(z),tkbuffer);
  64.  (*z).type=tc_symbol;
  65.  strcpy(PNAME(z),tkbuffer);
  66.  (*z).storage_as.symbol.vcell = cons(unbound_marker,NIL);
  67.  counter++;
  68.  no_interrupt(flag);
  69.  return(z);}
  70.  
  71. LISP symbolp(LISP x)
  72. {if SYMBOLP(x) return(truth);
  73.  return(NIL);}
  74.  
  75. LISP macrop(LISP x)
  76. {if TYPEP(x,tc_macro) return(truth);
  77.  return(NIL);}
  78.  
  79. char *must_malloc(unsigned long size)
  80. {char *tmp;
  81.  tmp = (char *) malloc(size);
  82.  if (tmp == (char *)NULL)
  83.    {gc_for_newcell();
  84.     tmp = (char *) malloc(size);
  85.     if (tmp == (char *)NULL)
  86.         err("failed to allocate storage from system",NIL,ERR_GEN);}
  87.  return(tmp);}
  88.  
  89. LISP gen_intern(char *name,long copyp)
  90. {LISP l,sym,sl;
  91.  char *cname;
  92.  long n,c,flag;
  93.  unsigned hash;
  94.  flag = no_interrupt(1);
  95.  hash = 0;
  96.  n = obarray_dim;
  97.  cname = name;
  98.  while(c = *cname++) hash = ((hash * 17) ^ c) % n;
  99.  sl = obarray[hash];
  100.  for(l=sl;CONSP(l);l=CDR(l))
  101.    if (strcmp(name,PNAME(CAR(l))) == 0)
  102.      {no_interrupt(flag);
  103.       return(CAR(l));}
  104.  if (copyp == 1)
  105.    {cname = must_malloc(strlen(name)+1);
  106.     strcpy(cname,name);}
  107.  else
  108.    cname = name;
  109.  sym = symcons(cname,unbound_marker);
  110.  obarray[hash] = cons(sym,sl);
  111.  no_interrupt(flag);
  112.  return(sym);}
  113.  
  114. LISP cintern(char *name)
  115. {return(gen_intern(name,0));}
  116.  
  117. LISP rintern(char *name)
  118. {return(gen_intern(name,1));}
  119.  
  120. LISP proplookup(LISP name, LISP prop)
  121. {LISP frame,tmp;
  122.  for(frame = PROPL(name);NNULLP(frame);frame=cdr(frame))
  123.   {tmp = car(frame);
  124.    if(EQ(car(tmp),prop)) return(tmp);}   
  125.  return(NIL);}
  126.  
  127. LISP putprop(LISP name, LISP val, LISP prop)
  128. {LISP tmp;
  129.  if (NSYMBOLP(name)) err("putprop",name,ERR_FIRST | ERR_NSYM);
  130.  if (NSYMBOLP(prop)) err("putprop",prop,ERR_THIRD | ERR_NSYM);
  131.  tmp = proplookup(name,prop);
  132.  if NULLP(tmp)
  133.    PROPL(name) = cons(cons(prop,val),PROPL(name));
  134.  else
  135.    CDR(tmp) = val;
  136.  return(PROPL(name));}
  137.  
  138. LISP getprop(LISP name, LISP prop)
  139. {LISP tmp;
  140.  if (NSYMBOLP(name)) err("getprop",name,ERR_FIRST | ERR_NSYM);
  141.  if (NSYMBOLP(prop)) err("getprop",prop,ERR_SECOND | ERR_NSYM);
  142.  tmp = proplookup(name,prop);
  143.  if NNULLP(tmp)
  144.    tmp = cdr(tmp);
  145.  return(tmp);}
  146.  
  147. LISP proplist(LISP name)
  148. {if (NSYMBOLP(name)) err("proplist",name,ERR_GEN_ARG | ERR_NSYM);
  149.  return(PROPL(name));}
  150.  
  151. LISP remprop(LISP name, LISP prop)
  152. {LISP frame,tmp,ptr;
  153.  if (NSYMBOLP(name)) err("remprop",name,ERR_FIRST | ERR_NSYM);
  154.  if (NSYMBOLP(prop)) err("remprop",prop,ERR_SECOND | ERR_NSYM);
  155.  for(ptr = NIL,frame = PROPL(name);NNULLP(frame);ptr = frame,frame = cdr(frame))
  156.   {tmp = car(frame);
  157.    if(EQ(car(tmp),prop))
  158.     {if(NULLP(ptr))
  159.        PROPL(name) = cdr(frame);
  160.      else
  161.        CDR(ptr) =  cdr(frame);
  162.      return(PROPL(name));}}   
  163.  return(NIL);}
  164.  
  165. LISP subrcons(long type, char *name, LISP (*f)())
  166. {LISP z;
  167.  NEWCELL(z,type);
  168.  (*z).storage_as.subr.name = name;
  169.  (*z).storage_as.subr.f = f;
  170.  return(z);}
  171.  
  172. LISP closure(LISP env,LISP code)
  173. {LISP z;
  174.  NEWCELL(z,tc_closure);
  175.  DEFENV(z) = env;
  176.  CODE(z) = code;
  177.  return(z);}
  178.  
  179. LISP fluidclosure(LISP env,LISP code)
  180. {LISP z;
  181.  NEWCELL(z,tc_fluidclosure);
  182.  DEFENV(z) = env;
  183.  CODE(z) = code;
  184.  return(z);}
  185.  
  186. LISP rec_closure(LISP env,LISP code)
  187. {LISP z;
  188.  NEWCELL(z,tc_rec);
  189.  DEFENV(z) = env;
  190.  CODE(z) = code;
  191.  return(z);}
  192.  
  193. LISP breakpoint(LISP form,LISP env)
  194. {LISP mes,irr;
  195.  FILE *out;
  196.  mes=leval(car(form),env);
  197.  irr=leval(car(cdr(form)),env);
  198.  out=get_cur_out();
  199.  fput_st(out,"BREAK-POINT entered:\n");
  200.  ldisplayf(mes,out);
  201.  fput_st(out,"\n");
  202.  lprin1f(irr,out);
  203.  fput_st(out,"\n");
  204.  setv(sym_errobj,irr);
  205.  setv(cintern("*cargs*"),form);
  206.  setv(cintern("*cenv*"),env);
  207.  setv(cintern("*lasterr*"),mes);
  208.  mes=apply_proc(VCELL(sym_inspect),NIL,NIL);
  209.  setv(cintern("*cargs*"),NIL);
  210.  setv(cintern("*cenv*"),NIL);
  211.  return(mes);}
  212.  
  213. LISP asctosym(LISP arg)
  214. {char str[4]=" ";
  215.  if(NINTNUMP(arg))err("ascii->symbol",arg,ERR_GEN_ARG | ERR_NINT);
  216.  if((INTNM(arg)<0)||(INTNM(arg)>255))
  217.        err("ascii->symbol",arg,ERR_GEN_ARG | ERR_NINT);
  218.  str[0]=(char)INTNM(arg);
  219.  return(rintern(str));}
  220.  
  221. LISP symtoasc(LISP arg)
  222. {if(NSYMBOLP(arg))err("symbol->ascii",arg,ERR_GEN_ARG | ERR_NSYM);
  223.  return(intcons((long)*PNAME(arg)));}
  224.  
  225. LISP install(LISP proc,LISP code)
  226. {if(NTYPEP(proc,tc_closure) && NTYPEP(proc,tc_rec) && NTYPEP(proc,tc_fluidclosure))
  227.    err("set-procedure-code!",proc,ERR_FIRST | ERR_NPRO);
  228.  CODE(proc)=code;
  229.  return(proc);}
  230.  
  231.